import conf
import time
import argparse
import multiprocessing
from task import Task
from conf import MiniAppConfig, WxConfig, ZfbConfig, BdConfig, KAppConfig
from utils.common import CrawlerLogger, str2bool, uninstall_appium_app
from utils.common import init_logging
from trafficMonitor.traffic_monitor import main as trafficMonitor


def startTrafficMonitor(applet_name, pageUrl):
    trafficMonitorProcess = multiprocessing.Process(target=trafficMonitor, args=(applet_name, pageUrl))
    trafficMonitorProcess.start()
    time.sleep(3)


def startDynamicExerciser():
    task = Task(conf.MINIAPP_CONF.applets_name.value)
    task.run()


def main(): 

    CrawlerLogger.info(f"[*] 启动 trafficMonitor 进程...")
    startTrafficMonitor(conf.MINIAPP_CONF.applets_name, conf.MINIAPP_CONF.pageUrl)

    CrawlerLogger.info(f"[*] 启动 dynamicExerciser 进程...")
    startDynamicExerciser()

    # killall()
    # generate_report()


if __name__ == "__main__":
    parser = argparse.ArgumentParser(description='A powerful Lightweight Test Generator for WeChat Mini Programs',
                                     formatter_class=argparse.RawDescriptionHelpFormatter)
    parser.add_argument("--platform", "-p", metavar="wx", type=str, nargs="?",
                        help="The Small program platform", dest="platform", required=True)
    parser.add_argument("--webview", "-w", metavar="True", type=str2bool, nargs="?",
                        help="The exploration mode", dest="webview_mode", required=True)
    parser.add_argument("--applets-name", "-u", metavar="国务院", type=str, nargs="?",
                        help="The target applets", dest="applets_name", required=True)
    parser.add_argument("--log-dictionary-name", "-l", metavar="logs", type=str, nargs="?",
                        help="The log dictionary name", dest="log_dictionary_name", required=True)
    parser.add_argument("--device-id", "-d", metavar="8ABY0H7HM", type=str, nargs="?",
                        help="The device id", dest="device_id", required=True)
    parser.add_argument("--log-level", choices=["debug", "info"], type=str, nargs="?", default="info",
                        help="Log level of crawler. Default: info", dest="log_level", required=False)
    args = parser.parse_args()

    applets_name = args.applets_name
    platform = args.platform
    webview_mode = args.webview_mode
    device_id = args.device_id
    log_dictionary_name = args.log_dictionary_name

    if platform.lower() == "wx":
        conf.CLIENT_SIDE_CONF = WxConfig()
    elif platform.lower() == 'zfb':
        conf.CLIENT_SIDE_CONF = ZfbConfig()
    elif platform.lower() == 'bd':
        conf.CLIENT_SIDE_CONF = BdConfig()
    elif platform.lower() == 'kuai':
        conf.CLIENT_SIDE_CONF = KAppConfig()

    if webview_mode:
        conf.WEBVIEW_MODE = True
    else:
        conf.WEBVIEW_MODE = False
    conf.CLIENT_SIDE_CONF.DEVICE_ID = device_id
    conf.LOG_DICTIONARY_NAME = log_dictionary_name

    init_logging(args.log_level, platform.lower(), applets_name)
    if conf.CLIENT_SIDE_CONF.PLATFORM != "kuai":
        uninstall_appium_app()

    manager = multiprocessing.Manager()
    conf.MINIAPP_CONF = MiniAppConfig(manager)
    conf.MINIAPP_CONF.applets_name.value = applets_name
    
    main()